package demo1;

import java.util.HashMap;
import java.util.Map;

class  Node{
    public int val;
    public Node next;
    public Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
public class Test2 {
    public Node copyRandomList(Node head) {
        Map<Node,Node> map = new HashMap();
        Node cur = head;
        //先创建好与原链表每个节点的val值相同的节点
        while(cur != null){
            Node node = new Node(cur.val);
            map.put(cur,node);//将原链表节点和新链表节点的对应关系传到map中
            cur = cur.next;
        }
        cur = head;
        while(cur != null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }

    /*如果不用Map,用链表*/
    public static void main(String[] args) {

    }
}
